/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.framework.base.web;

import java.beans.PropertyEditorSupport;
import java.sql.Time;
import java.text.SimpleDateFormat;

import com.xw.util.AppConstant;

/**
 * The Class SqlDateEditor.
 *
 * @author Xi Wei
 * @version 1.0
 * @since 1.0
 */
public class SqlTimeEditor extends PropertyEditorSupport {

	/* (non-Javadoc)
	 * @see java.beans.PropertyEditorSupport#getAsText()
	 */
	public String getAsText() {
		Object obj = getValue();
		if(obj == null){
			return "";
		}
		Time value = null;
		value = (Time) getValue();

		SimpleDateFormat df = new SimpleDateFormat(AppConstant.DEFAULT_TIME_FORMAT);
		String str = df.format(value);
		return str;
	}

	/* (non-Javadoc)
	 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
	 */
	public void setAsText(String text) throws IllegalArgumentException {
		Time value = null;
		System.out.println("======= text ======= " + text);
		if (null != text && !text.equals("")) {
			SimpleDateFormat df = new SimpleDateFormat(AppConstant.DEFAULT_TIME_FORMAT);
			
			try {
				System.out.println("======== df.parse(text) ====== " + df.parse(text));
				value = new Time(df.parse(text).getTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("======== value ====== " + value);
		setValue(value);
	}
}
